package work.konbai.education.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.annotation.PreAuthorize;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import work.konbai.basic.vo.MemberVO;
import work.konbai.education.dto.CheckDTO;
import work.konbai.education.entity.HomeworkDetail;
import work.konbai.education.service.IHomeworkDetailService;
import work.konbai.education.vo.HomeworkDetailVO;

import javax.validation.Valid;
import java.util.List;

/**
 * 作业详情信息表 控制器
 *
 * @author KonBAI
 * @version 1.0
 * Create by 2021/3/8 10:35
 */
@RestController
@AllArgsConstructor
@RequestMapping("/homework-detail")
@Api(value = "作业详情信息表", tags = "作业详情信息表接口")
public class HomeworkDetailController {

	private final IHomeworkDetailService homeworkDetailService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入homeworkDetail")
	public R<HomeworkDetail> detail(HomeworkDetail homeworkDetail) {
		return R.data(homeworkDetailService.getOne(Condition.getQueryWrapper(homeworkDetail)));
	}

	/**
	 * 分页 作业详情信息表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入homeworkDetail")
	public R<IPage<HomeworkDetail>> list(HomeworkDetail homeworkDetail, Query query) {
		return R.data(homeworkDetailService.page(Condition.getPage(query), Condition.getQueryWrapper(homeworkDetail)));
	}

	/**
	 * 新增 作业详情信息表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入homeworkDetail")
	public R save(@Valid @RequestBody HomeworkDetail homeworkDetail) {
		return R.status(homeworkDetailService.save(homeworkDetail));
	}

	/**
	 * 修改 作业详情信息表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入homeworkDetail")
	public R update(@Valid @RequestBody HomeworkDetail homeworkDetail) {
		return R.status(homeworkDetailService.updateById(homeworkDetail));
	}

	/**
	 * 新增或修改 作业详情信息表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入homeworkDetail")
	public R submit(@Valid @RequestBody HomeworkDetail homeworkDetail) {
		return R.status(homeworkDetailService.saveOrUpdate(homeworkDetail));
	}


	/**
	 * 删除 作业详情信息表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(homeworkDetailService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 列表 作业验收 课程所有成员
	 */
	@GetMapping("/checkdata-list")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "列表", notes = "传入courseId")
	@PreAuthorize("hasPermission('course_view')")
	public R<List<MemberVO>> checkDataList(@ApiParam(value = "课程表ID", required = true) @RequestParam Long courseId) {
		return R.data(homeworkDetailService.checkDataList(courseId));
	}

	/**
	 * 列表 作业验收 已提交
	 */
	@GetMapping("/submitted-list")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "列表", notes = "传入homeworkId")
	public R<List<MemberVO>> submittedList(@ApiParam(value = "作业表ID", required = true) @RequestParam Long homeworkId) {
		return R.data(homeworkDetailService.submittedList(homeworkId));
	}

	/**
	 * 新增或修改 作业详情信息表 已提交
	 */
	@PostMapping("/check-submit")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "新增或修改", notes = "传入attendanceDetail")
	public R signSubmit(@Valid @RequestBody CheckDTO checkDTO) {
		return R.status(homeworkDetailService.checkSubmit(checkDTO));
	}

	/**
	 * 分页 作业成绩查询
	 */
	@GetMapping("/scoredata-list")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "列表", notes = "传入homeworkId")
	@PreAuth("hasRole('administrator')")
	public R<IPage<HomeworkDetailVO>> scoreDataPage(@ApiParam(value = "作业表ID", required = true) @RequestParam Long homeworkId, Query query) {
		return R.data(homeworkDetailService.scoreDataPage(Condition.getPage(query), homeworkId));
	}

	/**
	 * 修改 作业详情信息表
	 */
	@PostMapping("/update-score")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "修改", notes = "传入homeworkDetailList")
	public R updateScoreData(@Valid @RequestBody List<HomeworkDetail> homeworkDetailList) {
		return R.status(homeworkDetailService.updateBatchById(homeworkDetailList));
	}
}
